<template>
  <view class="page container">
    <u-form
      :model="basicForm"
      :rules="rules"
      labelPosition="top"
      ref="form"
      labelWidth="auto"
      :labelStyle="{
        fontSize: '14px',
        color: '#B2B2B2',
        lineHight: '20px',
      }"
    >
      <view class="basic-style">
        <u-form-item label="项目成员" prop="projectMember" required>
          <h-select-person-input-popup
            v-model="basicForm.projectMember"
						:disabled="basicForm.isProjectLeading"
            style="width: 100%"
            placeholder="请选择项目成员"
            searchPlaceholder="请输入"
            border="surround"
            suffixIcon="arrow-down-fill"
            :suffixIconStyle="{ color: '#B2B2B2' }"
            @change="handleMemberChange"
            @select="handleMemberSelect"
          ></h-select-person-input-popup>
        </u-form-item>
        <u-form-item label="分配合同额(元)" prop="contractAmount" required>
          <h-number-input
            placeholder="请输入分配合同额"
            v-model="basicForm.contractAmount"
            :digit="2"
            :max="99999999"
            :min="0"
            @change="amountInput('contract')"
            suffixIcon="edit-pen-fill"
          ></h-number-input>
        </u-form-item>
        <u-form-item label="合同额占比(%)" prop="contractRate">
          <h-number-input
            disabled
            placeholder="请输入合同额占比"
            v-model="basicForm.contractRate"
            :digit="2"
            :max="99999999"
            :min="0"
          ></h-number-input>
        </u-form-item>
        <u-form-item label="工时单价(元)" prop="dayCost">
          <h-number-input
            disabled
            placeholder="工资标准/21.75"
            v-model="basicForm.dayCost"
            :digit="2"
            :max="99999999"
            :min="1"
          ></h-number-input>
        </u-form-item>
        <u-form-item label="预算工时(小时)" prop="expectWorkTime" required>
          <h-number-input
            suffixIcon="edit-pen-fill"
            placeholder="请输入预算工时"
            v-model="basicForm.expectWorkTime"
            :digit="1"
            :max="99999999"
            :min="0"
          ></h-number-input>
        </u-form-item>
        <u-form-item label="项目绩效(元)" prop="projectPerformance" required>
          <h-number-input
            suffixIcon="edit-pen-fill"
            placeholder="请输入项目绩效"
            v-model="basicForm.projectPerformance"
            :digit="2"
            :max="99999999"
            :min="0"
          ></h-number-input>
        </u-form-item>
        <u-form-item label="人工成本(元)" prop="laborCost" required>
          <h-number-input
            disabled
            placeholder="工时单价*预算工时/8+项目绩效"
            v-model="basicForm.laborCost"
            :digit="2"
            :max="99999999"
            :min="0"
          ></h-number-input>
        </u-form-item>
        <!-- <u-form-item label="分配合同额(元)" prop="contractAmount">
          <u--input
            type="number"
            v-number-normal
            placeholder="请输入分配合同额"
            v-model="basicForm.contractAmount"
            @change="amountInput('contract')"
          ></u--input>
        </u-form-item> -->
        <!-- <u-form-item label="合同额占比" prop="contractRate">
          <u--input disabled type="number" placeholder="请输入合同额占比" v-model="basicForm.contractRate"></u--input>
        </u-form-item> -->
        <!-- <u-form-item label="工时单价(元)" prop="dayCost">
          <u--input disabled placeholder="工资标准/21.75" v-model="basicForm.dayCost"></u--input>
        </u-form-item>
        <u-form-item label="预算工时（人天）" prop="expectWorkTime" required>
          <u--input type="number" v-number-normal placeholder="请输入预算工时" v-model="basicForm.expectWorkTime"></u--input>
        </u-form-item>
        <u-form-item label="项目绩效(元)" prop="projectPerformance" required>
          <u--input type="number" v-number-normal placeholder="请输入项目绩效" v-model="basicForm.projectPerformance"></u--input>
        </u-form-item>
        <u-form-item label="人工成本(元)" prop="laborCost">
          <u--input type="number" v-number-normal placeholder="工时单价*预算工时+项目绩效" v-model="basicForm.laborCost"></u--input>
        </u-form-item> -->
      </view>

      <view class="space-x-1 mt-1" style="display: flex">
        <u-button type="primary" text="关闭" plain :loading="loading" @click="onClose"></u-button>
				<u-button type="primary" text="保存" :loading="loading" @click="onSubmit"></u-button>
      </view>
    </u-form>
  </view>
</template>

<script>
import http from '@/utils/request'
import Decimal from 'decimal.js'
export default {
  name: 'MemberForm',
  props: {
    teamId: {
      type: [Number, String],
      default: '',
    },
    companyId: {
      type: [Number, String],
      default: '',
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    datailDisabled: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      default: null,
    },
    model: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      smallList: [],
      loading: false,
      basicForm: {
        projectMember: '',
        contractAmount: '',
        contractRate: '',
        dayCost: '',
        expectWorkTime: '',
        projectPerformance: '',
        laborCost: '',
      },
      rules: {
        projectMember: [{ required: true, message: '请选择项目成员!' }],
        expectWorkTime: [{ required: true, message: '请输入预算工时!' }],
        projectPerformance: [{ required: true, message: '请输入项目绩效!' }],
        contractAmount: [{ required: true, message: '请输入分配合同额!' }],
      },
    }
  },
  created() {},
  computed: {
    laborCost() {
      // return (parseFloat(this.basicForm.dayCost) || 0) * (this.basicForm.expectWorkTime || 0) + (this.basicForm.projectPerformance || 0)
			
      function parse(num) {
        return Number(num) || 0
      }
      return new Decimal(parse(this.basicForm.dayCost))
        .mul(parse(this.basicForm.expectWorkTime)).div(8)
        .add(parse(this.basicForm.projectPerformance))
        .toNumber()
    },
  },
  methods: {
    amountInput(kind) {
      if (kind === 'contract') {
        this.basicForm.contractRate = parseFloat(((this.basicForm.contractAmount * 100) / this.model.contractAmount).toFixed(2)) || 0.00
				// this.basicForm.contractRate = this.basicForm.contractRate || ''
      }
    },
    handleMemberChange(item) {
      if (!item) return void 0
      http.get('/api/erp/files/employee/erpEmployeeFile/queryPayGradeByEmpId', { empId: item.empId }).then((response) => {
        this.$set(this.basicForm, 'dayCost', response.result)
      })
    },
    handleMemberSelect(item) {
      this.$set(this.basicForm, 'projectMember_dictText', item.data[0].realname)
    },
    materialNameBack(material) {
      this.$set(this.basicForm, 'materialBillId', material.materialBill) // 物料编码
      this.$set(this.basicForm, 'specification', material.specification) // 规格型号
      this.$set(this.basicForm, 'unit', material.unit) // 单位
      this.$set(this.basicForm, 'brand', material.brand) // 品牌
      this.$set(this.basicForm, 'recipientsNum', null) //数量
      this.$set(this.basicForm, 'materialName_dictText', material.materialName)
    },
    reset() {
      this.$refs.form.resetFields()
    },
    async onSubmit() {
      await this.$refs.form.validate()
      if (!this.index) {
        this.$emit('submit', [
          {
            id: parseInt(Math.random() * 1000000000000),
            ...this.basicForm,
          },
          this.index,
        ])
      } else {
        this.$emit('submit', [this.basicForm, this.index])
      }
      this.$refs.form.resetFields()
    },
    onClose() {
      this.$emit('close')
      this.$refs.form.resetFields()
    },
  },
  watch: {
    laborCost: {
			handler (val) {
				this.basicForm.laborCost = (Number(val) || 0).toFixed(2)
			},
			immediate: true,
			deep: true
		},
  },
}
</script>

<style scoped></style>
